package com.sky.financelake.adapter;

import android.graphics.drawable.AnimationDrawable;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.sky.financelake.databinding.ItemCommonFooterBinding;
import com.sky.financelake.retrofit.ApiResponse;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected static String TAG = BaseAdapter.class.getCanonicalName();
    public List<T> datas = new ArrayList<>();

    // 普通布局
    public static final int TYPE_ITEM = 1;
    // 脚布局
    public static final int TYPE_FOOTER = 2;
    // 当前加载状态，默认为加载完成
    public int loadStatus = 1;
    // 正在加载
    public static final int LOADING = 1;
    // 加载完成
    public static final int LOADING_COMPLETE = 2;
    // 加载到底
    public static final int LOADING_END = 3;

    /**
     * 底部加载更多菊花drawable
     */
    protected AnimationDrawable loadAd;

    public OnItemClickListener mOnItemClickListener;

    protected LifecycleOwner owner;

    protected LiveData<ApiResponse<Object>> liveData;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return datas.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为FooterView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @FunctionalInterface
    public interface OnItemClickListener {
        void onClick(final Integer position, final Object o);
    }

    public void setOnItemClickListener(@NonNull final OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public class FootViewHolder extends RecyclerView.ViewHolder {

        AppCompatImageView loading;
        AppCompatTextView loadEnd;

        FootViewHolder(@NonNull ItemCommonFooterBinding binding) {
            super(binding.getRoot());
            loading = (AppCompatImageView) binding.loading;
            loadEnd = (AppCompatTextView) binding.loadEnd;
            loadAd = (AnimationDrawable) loading.getDrawable();
        }
    }
}
